package com.bingxue.edu.management.shop.model.entity;

import com.bingxue.edu.common.enums.OperateSourceEnum;
import com.bingxue.edu.common.enums.OrderStatusEnum;
import com.bingxue.edu.common.enums.PayTypeEnum;
import com.bingxue.edu.framework.common.model.BusinessEntity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Table;
import com.mybatisflex.core.handler.JacksonTypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 购课订单实体
 *
 * @author panbo
 * @since 2024/08/27 14:31
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Table("shop_order")
public class Order extends BusinessEntity {

    @Schema(description = "学生id")
    private String studentId;

    @Schema(description = "原始总价")
    private BigDecimal originalTotalPrice;

    @Schema(description = "实际总价")
    private BigDecimal actualTotalPrice;

    @Schema(description = "折扣信息")
    @Column(typeHandler = JacksonTypeHandler.class)
    private Map<String, BigDecimal> discountDetail;

    @Schema(description = "订单状态")
    private OrderStatusEnum status;

    @Schema(description = "是否支付")
    private Boolean payOff;

    @Schema(description = "支付方式")
    private PayTypeEnum payType;

    @Schema(description = "支付时间")
    private LocalDateTime payTime;

    @Schema(description = "外部交易号")
    private String outTradeNo;

    @Schema(description = "财务账户")
    private String finAccountId;

    @Schema(description = "取消时间")
    private LocalDateTime cancelTime;

//    @Schema(description = "是否退款")
//    private Boolean refunded;

    @Schema(description = "退费金额")
    private BigDecimal refundPrice;

    @Schema(description = "备注（内部）")
    private String remark;

    @Schema(description = "备注（外部）")
    private String remarkOut;

    @Schema(name = "操作来源")
    private OperateSourceEnum operateSource;

    @Schema(name = "支付详情")
    @Column(isLarge = true)
    private String payOrderDetail;

    @Schema(description = "有效期截止时间")
    private LocalDateTime endValidTime;

    @Schema(description = "订单是否取消")
    public Boolean isCancel() {
        return cancelTime != null;
    }

    public void addDiscountDetail(String key, BigDecimal value) {
        if (discountDetail == null) {
            discountDetail = new HashMap<>();
        }
        discountDetail.put(key, value);
    }

    @Schema(description = "优惠总额")
    public BigDecimal getDiscountTotalPrice() {
        return originalTotalPrice.subtract(actualTotalPrice);
    }
}
